package com.jxc.servlet.ServletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/ServletContext/Context")
public class ServletContextInitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 实例化一个ServletContext对象
        ServletContext context = req.getServletContext();

        // 利用实例化的ServletContext对象，调用getInitParameter()方法可以提取web.xml设定好的初始化信息，这样更加便于维护
        String title = context.getInitParameter("title");
        String footer = context.getInitParameter("footer");

        // 设置自定义的属性
        context.setAttribute("title",title);
        context.setAttribute("footer",footer);

        resp.getWriter().print("<h1>init successful!</h1>");
    }
}
